﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.IO;
using System.Text;
using System.Configuration;

namespace DeepTalkRenderService.Activities
{
    /// <summary>
    /// Simplistic code that builds a directory from a settings guy and a
    /// ID type object that knows about ToString().
    /// </summary>
	public partial class DirectoryByIDAndSettings: Activity
	{
        /// <summary>
        /// Get/Set the configuration settings name that we will put the string from.
        /// </summary>
        public string AppConfigSettingsName
        {
            get { return (string)GetValue(AppConfigSettingsNameProperty); }
            set { SetValue(AppConfigSettingsNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for AppConfigSettingsName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AppConfigSettingsNameProperty =
            DependencyProperty.Register("AppConfigSettingsName", typeof(string), typeof(DirectoryByIDAndSettings), new PropertyMetadata(null));

        /// <summary>
        /// The object that is used to add to the string.
        /// </summary>
        public object ItemToGetAsString
        {
            get { return (object)GetValue(ItemToGetAsStringProperty); }
            set { SetValue(ItemToGetAsStringProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ItemToGetAsString.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemToGetAsStringProperty =
            DependencyProperty.Register("ItemToGetAsString", typeof(object), typeof(DirectoryByIDAndSettings), new PropertyMetadata(null));

        /// <summary>
        /// Formatting string used to build the resulting directory info object.
        /// </summary>
        public string FormatString
        {
            get { return (string)GetValue(FormatStringProperty); }
            set { SetValue(FormatStringProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FormatString.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FormatStringProperty =
            DependencyProperty.Register("FormatString", typeof(string), typeof(DirectoryByIDAndSettings), new PropertyMetadata(null));

        /// <summary>
        /// The directory that is the result of putting these guys together.
        /// </summary>
        public DirectoryInfo ResultDirectory
        {
            get { return (DirectoryInfo)GetValue(ResultDirectoryProperty); }
            set { SetValue(ResultDirectoryProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ResultDirectory.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ResultDirectoryProperty =
            DependencyProperty.Register("ResultDirectory", typeof(DirectoryInfo), typeof(DirectoryByIDAndSettings), new PropertyMetadata(null));

		public DirectoryByIDAndSettings()
		{
			InitializeComponent();
            FormatString = "{0}\\{1}";
		}

        /// <summary>
        /// Do the directory build!
        /// </summary>
        /// <param name="executionContext"></param>
        /// <returns></returns>
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            StringBuilder bld = new StringBuilder();
            string configSetting = ConfigurationSettings.AppSettings[AppConfigSettingsName];
            bld.AppendFormat(FormatString, configSetting, ItemToGetAsString.ToString());
            ResultDirectory = new DirectoryInfo(bld.ToString());
            return ActivityExecutionStatus.Closed;
        }
	}
}
